#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <gtk/gtk.h>

#define INTERVAL 1000
#define TYPE_TIME 0
#define TYPE_NUM 1
#define TYPE_SUM 2

int thread_type;
GtkTextBuffer *buffer;

void clear() {
    buffer = NULL;
    gtk_main_quit();
}

gboolean update_ui(gpointer data) {
    if (buffer) {
        gtk_text_buffer_set_text(buffer, data, -1);
    }

    return FALSE;
}

// time part
// number part
gpointer update_thread(gpointer data) {
    time_t raw_time;
    struct tm *timeinfo;
    int num= 0;
    int sum = 0;
    char str[30];
    while(TRUE) {
        if (thread_type == TYPE_TIME) {
            time(&raw_time);
            timeinfo = localtime(&raw_time);
            strftime(str, 30, "%H:%M:%S", timeinfo);
        }
        else if (thread_type == TYPE_NUM) {
            if (num== 10) {
                num = 0;
            }
            sprintf(str, "Current Number %d", num++);
        }
        else if (thread_type == TYPE_SUM) {
            if (num == 1000) {
                num = 1;
                sum = 0;
            }
            sum += ++num;
            sprintf(str, "Current Sum %d", sum);
        }
        update_ui(str);
        if (!buffer) {
            g_thread_exit(NULL);
        }
        sleep(1);
    }
    return NULL;
}

int show(int *argc, char *** argv, int type) {
    gtk_init(argc, argv);

    GtkBuilder *builder = gtk_builder_new_from_file("layout.glade");

    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
    if (type == TYPE_TIME) {
        gtk_window_set_title((GtkWindow*)window, "Current Time");
    }
    else if (type == TYPE_NUM) {
        gtk_window_set_title((GtkWindow*)window, "Number from 0 to 9");
    }
    else if (type == TYPE_SUM) {
        gtk_window_set_title((GtkWindow*)window, "Sum of 1 to 1000");
    }
    thread_type = type;

    GtkWidget *textView = GTK_WIDGET(gtk_builder_get_object(builder, "textview"));
    GtkWidget *button = GTK_WIDGET(gtk_builder_get_object(builder, "button"));
    // bind signal
    g_signal_connect(window, "delete_event", G_CALLBACK(clear), NULL);
    g_signal_connect(button, "clicked", G_CALLBACK(clear), NULL);

    // set textview background color to yellow
    GdkColor yellowColor;
    yellowColor.red = 0xffff;
    yellowColor.green = 0xffff;
    yellowColor.blue = 0;
    gtk_widget_modify_bg(textView, GTK_STATE_NORMAL, &yellowColor);


    buffer = gtk_text_view_get_buffer((GtkTextView*)textView);


    g_thread_new("update number", update_thread, NULL);

    g_object_unref(builder);
    gtk_widget_show(window);

    gtk_main();

    return 0;
}


int main(int argc, char *argv[]) {
    pid_t pid_time, pid_num, pid_sum;

    if ((pid_time = fork()) == 0) {
        // process for update time
        show(&argc, &argv, TYPE_TIME);
    }
    else {
        if ((pid_num = fork()) == 0) {
            // process for update number from 0 to 9
            show(&argc, &argv, TYPE_NUM);
        }
        else {
            if ((pid_sum = fork()) == 0) {
                //process for update sum of 1 to 1000
                show(&argc, &argv, TYPE_SUM);
            }
            else {
                wait(0);
                wait(0);
                wait(0);
                printf("Thanks for use.\n");
            }
        }
    }

    return 0;
}

